package com.xique.wxclient.service.biz.service.impl;

import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.MathUtil;
import com.xique.wxclient.bean.domain.*;
import com.xique.wxclient.bean.request.WxAddVisitorRequest;
import com.xique.wxclient.bean.request.WxCancelVisitorRequest;
import com.xique.wxclient.service.biz.service.IVisitorBizService;
import com.xique.wxclient.service.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VisitorBizServiceImpl implements IVisitorBizService {

    @Autowired
    private IParkCarMngtypeService parkCarMngtypeService;

    @Autowired
    private IParkCarService parkCarService;

    @Autowired
    private IVisitorService visitorService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IParkInfoService parkInfoService;

    /**
     * 保存访客车辆信息到车辆表中
     * 1、查询该车场的访客车车辆管理类型，如果有的话，取一种，如果没有的话，生成；（TODO）
     * 2、
     * @param request
     * @param user
     * @return
     */
    @Override
    public ParkCar saveVisitorCar(WxAddVisitorRequest request,User user) {
        /**1、询该车场的访客车管理类型*/
        ParkCarMngtype parkCarMngtype = new ParkCarMngtype();
        parkCarMngtype.setParkId(request.getParkId());
        parkCarMngtype.setMngType("3");
        parkCarMngtype.setStatus("normal");
        if ("2".equals(request.getVisitorCarType())) {//收费访客
            parkCarMngtype.setIsFee("Y");
        } else if ("3".equals(request.getVisitorCarType())) {//免费访客
            parkCarMngtype.setIsFee("N");
        }
        List<ParkCarMngtype> parkCarMngtypeList = parkCarMngtypeService.selectParkCarMngtypeList(parkCarMngtype);
        if(!CollectionUtils.isEmpty(parkCarMngtypeList)){
            parkCarMngtype = parkCarMngtypeList.get(0);
        }else{
            parkCarMngtype.setName("访客车");
            parkCarMngtypeService.insertParkCarMngtype(parkCarMngtype);
        }
        /**2、保存车辆信息，判断车牌是否存在，存在则更新，不存在则新增*/
        ParkCar parkCar = new ParkCar();
        parkCar.setParkId(request.getParkId());
        parkCar.setPlate(request.getVisitorCarPlate());
        parkCar.setBindPlates(request.getVisitorCarPlate());
        //开始日期如果不填，默认就是当前日期
        Date beginDate = null != request.getDate() ? DateUtils.parseDate(request.getDate()) : new Date();
        parkCar.setBeginDate(beginDate);
        parkCar.setRemainDays(request.getDays());
        parkCar.setUserName(user.getUserName());
        parkCar.setVisitorPhone(request.getVisitorPhone());
        parkCar.setVisitorName(request.getVisitorName());
        parkCar.setPhone(user.getPhone());
        parkCar.setRemark("外来访客车邀请");
        parkCar.setMngType("3");//邀请访客大类-3
        //车辆其他信息填充，因为下发到设备中，有些参数必填
        parkCar.setSpaces(1L);
        parkCar.setCarType("1");
        parkCar.setStatus("normal");
        parkCar.setEndDate(DateUtils.getNextDate(beginDate, null != request.getDays() ? request.getDays().intValue() : 0));
        parkCar.setMngTypeId(parkCarMngtype.getId());
        //查询车牌是否添加过，添加过，则更新。没有，则添加
        ParkCar parkCar1 = new ParkCar();
        parkCar1.setParkId(request.getParkId());
        parkCar1.setPlate(request.getVisitorCarPlate());
        List<ParkCar> parkCarList1 = parkCarService.selectParkCarList(parkCar1);
        log.info("parkCarList1:{}", parkCarList1);
        int count = 0;
        if (!CollectionUtils.isEmpty(parkCarList1)) {
            parkCar.setId(parkCarList1.get(0).getId());
            count = parkCarService.updateParkCar(parkCar);
        } else {
            count = parkCarService.insertParkCar(parkCar);
        }
        if(count > 0){
            return parkCar;
        }else{
            return null;
        }
    }

    /**
     * 保存访客信息
     * @param request
     * @return
     */
    @Override
    public Visitor saveVisitorInfo(WxAddVisitorRequest request) {
        Visitor visitor = new Visitor();
        visitor.setParkId(request.getParkId());
        visitor.setUserId(request.getUserId());
        visitor.setCustomId(System.currentTimeMillis()+ MathUtil.getRandom(19));
        visitor.setVisitorCarPlate(request.getVisitorCarPlate());
        visitor.setVisitorCarType(request.getVisitorCarType());
        visitor.setVisitorName(request.getVisitorName());
        visitor.setVisitorPhone(request.getVisitorPhone());
        //开始日期如果不填，默认就是当前日期
        Date beginDate = null != request.getDate() ? DateUtils.parseDate(request.getDate()) : new Date();
        Date endDate = DateUtils.getNextDate(beginDate, null != request.getDays() ? request.getDays().intValue() : 0);
        visitor.setStartTime(beginDate);
        visitor.setEndTime(endDate);
        visitor.setVerifyStatus("pass");
        int count = visitorService.insertVisitor(visitor);
        if(count > 0){
            return visitor;
        }else{
            return null;
        }
    }

    /**
     * 获取访客列表信息
     * @param userId
     * @return
     */
    @Override
    public List<Visitor> getVisitorList(Long userId) {
        Visitor visitor = new Visitor();
        visitor.setUserId(userId);
        List<Visitor> visitorList = visitorService.selectVisitorList(visitor);
        User user = userService.selectUserById(userId);
        Long parkId = user.getParkId();
        ParkInfo parkInfo = parkInfoService.selectParkInfoById(parkId);
        visitorList.forEach(item -> item.setParkName(parkInfo.getName()));
        return visitorList;
    }

    @Override
    public List<Visitor> getVisitorList(String userIds) {
        Visitor visitor = new Visitor();
        visitor.setUserIds(userIds);
        visitor.setStatus("1");
        return visitorService.selectVisitorList(visitor);
    }

    /**
     * 状态修改为停用
     * @param request
     * @return
     */
    @Override
    public Visitor cancelVisitor(WxCancelVisitorRequest request) {
       Visitor visitor = visitorService.selectVisitorById(request.getId());
       if(null != visitor){
           visitor.setStatus("0");
           visitorService.updateVisitor(visitor);
       }
       return visitor;
    }

}
